/* ***** BEGIN LICENSE BLOCK *****
 * Copyright (C) 2008-2011, The 100GET-E3-R3G Project Team.
 * 
 * This work has been funded by the Federal Ministry of Education
 * and Research of the Federal Republic of Germany
 * (BMBF Förderkennzeichen 01BP0775). It is part of the EUREKA project
 * "100 Gbit/s Carrier-Grade Ethernet Transport Technologies
 * (CELTIC CP4-001)". The authors alone are responsible for this work.
 *
 * See the file AUTHORS for details and contact information.
 * 
 * This file is part of MuLaViTo (Multi-Layer Visualization Tool).
 *
 * MuLaViTo is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License Version 3 or later
 * (the "GPL"), or the GNU Lesser General Public License Version 3 or later
 * (the "LGPL") as published by the Free Software Foundation.
 *
 * MuLaViTo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * or the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License and
 * GNU Lesser General Public License along with MuLaViTo; see the file
 * COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * ***** END LICENSE BLOCK ***** */
package mulavito.utils;

import java.awt.Color;
import java.io.File;

import javax.swing.JComponent;

import mulavito.gui.components.LayerViewer;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

/**
 * Use Batik library to export content of a {@link LayerViewer} to a <i>scalable
 * vector graphic</i> (SVG) image file.
 * 
 * @see http://xmlgraphics.apache.org/batik/
 * 
 * @author Michael Duelli
 * @since 2008-10-07
 */
public final class SVGExporter {
	public void export(JComponent component, File file) {
		if (component == null || file == null)
			throw new IllegalArgumentException();

		String fileName = file.getAbsolutePath();

		if (!fileName.endsWith(".svg"))
			fileName += ".svg";

		// Get a DOMImplementation.
		DOMImplementation domImpl = GenericDOMImplementation
				.getDOMImplementation();

		// Create an instance of org.w3c.dom.Document.
		String svgNS = "http://www.w3.org/2000/svg";
		Document doc = domImpl.createDocument(svgNS, "svg", null);

		SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
		ctx.setComment("Generated by MuLaViTo with Batik SVG");
		SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);

		// Prepare export: Exchange background color.
		Color c = component.getBackground();
		component.setBackground(Color.WHITE);
		component.paint(svgGenerator);
		component.setBackground(c);

		// Modify export.
		svgGenerator.setSVGCanvasSize(component.getSize());

		// Export.
		try {
			svgGenerator.stream(fileName);
		} catch (SVGGraphics2DIOException e) {
			e.printStackTrace();
		}
	}
}
